package 二叉树.左叶子之和;

import 链表.移除链表元素.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/2/10 17:12
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(3,new TreeNode(9),new TreeNode(20,new TreeNode(15),new TreeNode(7)));
        System.out.println(sumOfLeftLeaves(root));
    }

    public static int sumOfLeftLeaves(TreeNode root) {
        List<TreeNode> res = new ArrayList<>();
        int sum = 0;
        inorder(root,res,0);
        for (int i = 0; i < res.size(); i++) {
            sum += res.get(i).val;
        }
        return sum;
    }

    //先序遍历
    public static void inorder(TreeNode root,List<TreeNode> list,int target){
        if (root == null){
            return;
        }
        if (target == 1 && root.left == null && root.right == null){
            list.add(root);
        }
        inorder(root.left,list,1);
        inorder(root.right,list,0);
    }

}
